package com.ym.pms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.pms.dao.BuildingMapper;
import com.ym.pms.dao.ItemMapper;
import com.ym.pms.dao.RoomMapper;
import com.ym.pms.dao.SuiteMapper;
import com.ym.pms.getCoding.NewCoding;
import com.ym.pms.model.Building;
import com.ym.pms.model.Suite;
import com.ym.pms.service.RoomService;
import com.ym.pms.service.SuiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SuiteServiceImpl implements SuiteService {

    @Autowired
    SuiteMapper suDao;
    @Autowired
    BuildingMapper buDao;
    @Autowired
    ItemMapper itDao;
    @Autowired
    NewCoding newCoding;
    @Autowired
    RoomMapper rmDao;
    @Autowired
    RoomService rmService;

    @Override
    public int add(Suite suite) {
        suite.setSuCoding(getCoding());
        Building suBu = buDao.getSuBu(suite.getSuBucoding());
        suite.setSuItid(suBu.getBuItid());
        suite.setSuBuname(suBu.getBuName());
        suite.setSuRegion(suBu.getBuRegion());
        suite.setSuPa(suBu.getBuPads());
        suite.setSuBuua(suBu.getBuUarea());
        suite.setSuAuarea(suite.getSuUarea() - suite.getSuSarea());
        Date sDate = buDao.getSDate(suite.getSuBucoding());
        Date eDate = buDao.getEDate(suite.getSuBucoding());
        Date now = new Date();
        if(now.before(eDate) && now.after(sDate)){
            suite.setSuEstate("STARTUSING");
        }else{
            suite.setSuEstate("DEACTIVATE");
        }
        suite.setSuSouse("VACANT");
        int i = suDao.insertSelective(suite);
        return i;
    }

    @Override
    public int delete(String suCoding) {
        if(suCoding == null || suCoding.equals("")){
            return -1;
        }else if(findByCoding(suCoding) == null){
            return -2;
        }else{
            rmService.deleteAllBySuCoding(suCoding);
            return suDao.deleteByPrimaryKey(suCoding);
        }
    }

    @Override
    public int update(Suite suite) {
        if(suite.getSuCoding() == null || suite.getSuCoding().equals("")){
            return -1;
        }else if(findByCoding(suite.getSuCoding()) == null){
            return -2;
        }else{
            return suDao.updateByPrimaryKeySelective(suite);
        }
    }

    @Override
    public PageInfo findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Suite> suites = suDao.querySuiteList();
        PageInfo pageInfo = new PageInfo<Suite>(suites);
        return pageInfo;
    }

    @Override
    public Suite findByCoding(String suCoding) {
        return suDao.selectByPrimaryKey(suCoding);
    }

    @Override
    public PageInfo fuzzyQuery(Suite suite, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Suite> suites = suDao.fuzzyQuery(suite);
        PageInfo pageInfo = new PageInfo<Suite>(suites);
        return pageInfo;
    }

    @Override
    public PageInfo findByStartUsing(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Suite> suites = suDao.findByStartUsing();
        PageInfo pageInfo = new PageInfo<Suite>(suites);
        return pageInfo;
    }

    @Override
    public PageInfo findByStartUsingAndVacant(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Suite> suites = suDao.findByStartUsingAndVacant();
        PageInfo pageInfo = new PageInfo<Suite>(suites);
        return pageInfo;
    }

    @Override
    public int getMaxNo() {
        Integer BuildingMaxNo = suDao.getBuildingMaxNo();
        if (BuildingMaxNo == null) {
            return 0;
        } else {
            return BuildingMaxNo;
        }
    }

    @Override
    public String getCoding() {
        Integer maxNo = this.getMaxNo();
        maxNo += 1;
        String suCoding = newCoding.getCoding("SU", maxNo);
        return suCoding;
    }

    @Override
    public void deleteAllByBuCoding(String id) {
        List<String> allCoding = suDao.getAllCoding(id);
        for (String s : allCoding) {
            rmService.deleteAllBySuCoding(s);
        }
        suDao.deleteAllByBuCoding(id);
    }
}
